﻿using DotNetExtensions.AspNet.Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace AspNetSimple
{
    class NetworkSimple
    {
        public NetworkSimple()
        {
            #region Net Work Simple

            Telnet telnet = new Telnet();
            Network network = new Network();
            var dt1 = DateTime.Now;
            bool result = telnet.TryConnect("192.168.20.217", 54392);
            Console.WriteLine(result);

            CancellationTokenSource source = new CancellationTokenSource();
            var task = telnet.Listening("192.168.20.217", 44392, online =>
            {
                Console.WriteLine("{2}-{0}:{1}", DateTime.Now, online, "192.168.20.217");
            }, 200, source.Token);

            Timer timer = new Timer(new TimerCallback(obj =>
            {
                Console.WriteLine("Send Canceled");
                (obj as CancellationTokenSource).Cancel();
            }), source, 2 * 1000, 1 * 1000);

            task.ContinueWith(t =>
            {
                timer.Dispose();
                Console.WriteLine("Canceled");
            });

            var task2 = network.Listening("192.168.30.17", online =>
            {
                Console.WriteLine("{2}-{0}:{1}", DateTime.Now, online, "192.168.30.17");
            }, 200, source.Token);
            Console.WriteLine((DateTime.Now - dt1).TotalSeconds);
            #endregion
        }
    }
}
